package bsc.war.populate;

import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Map;
import bsc.ejb.entity.MapDrawingSettings;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.SRM;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.ScoreView;
import bsc.ejb.entity.Settings;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.MapDrawing;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class OaxacaSettings extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

        ScoreCard scoreCardRoot = delegate.findScoreCard(20L);
        ScoreCard scoreCardRootSS = delegate.findScoreCard(21L);

        Person personDesigner = delegate.findPerson(1L);
        ScoreDomain scoreDomainDesigner = delegate.findScoreDomain(1L);

         //Se asocia persona al dominio

        PersonScoreDomain personScoreDomainDesigner = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
        personScoreDomainDesigner.setPerson(personDesigner);
        personScoreDomainDesigner.setScoreDomain(scoreDomainDesigner);
        personScoreDomainDesigner.setProfile(PersonScoreDomain.Profile.DESIGNER);
        delegate.createPersonScoreDomain(personScoreDomainDesigner);




        Person person = delegate.findPerson(2L);
        ScoreDomain scoreDomain = delegate.findScoreDomain(2L);
        ScoreDomain scoreDomainSS = delegate.findScoreDomain(3L);


        //VIEW 1
        //SE ASOCIA EL VIEW
        ScoreView scoreView = (ScoreView) Factory.getBean("ScoreView");
        scoreView.setName("PED");
        scoreView.setDescription("Vista que contiene los Scorecards de PED");
        scoreView.setScoreCard(scoreCardRoot);
        scoreView.setScoreDomain(scoreDomain);
        delegate.createScoreView(scoreView);

        scoreCardRoot.setScoreview(scoreView);
        delegate.updateScoreCard(scoreCardRoot);

        //Se updatea el dominio

        scoreDomain.setScoreView(scoreView);
        delegate.updateScoreDomain(scoreDomain);


        //Se asocia persona al dominio

        PersonScoreDomain personScoreDomain = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
        personScoreDomain.setPerson(person);
        personScoreDomain.setScoreDomain(scoreDomain);
        personScoreDomain.setProfile(PersonScoreDomain.Profile.CORE_TEAM);
        delegate.createPersonScoreDomain(personScoreDomain);


       //VIEW 2


        //SE ASOCIA EL VIEW
        ScoreView scoreViewSS = (ScoreView) Factory.getBean("ScoreView");
        scoreViewSS.setName("SS");
        scoreViewSS.setDescription("Vista que contiene los Scorecards de SS");
        scoreViewSS.setScoreCard(scoreCardRootSS);
        scoreViewSS.setScoreDomain(scoreDomainSS);
        delegate.createScoreView(scoreViewSS);

        scoreCardRootSS.setScoreview(scoreViewSS);
        delegate.updateScoreCard(scoreCardRootSS);

        //Se updatea el dominio

        scoreDomainSS.setScoreView(scoreViewSS);
        delegate.updateScoreDomain(scoreDomainSS);

        //Se asocia persona al dominio

        PersonScoreDomain personScoreDomainSS = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
        personScoreDomainSS.setPerson(person);
        personScoreDomainSS.setScoreDomain(scoreDomainSS);
        personScoreDomainSS.setProfile(PersonScoreDomain.Profile.CORE_TEAM);
        delegate.createPersonScoreDomain(personScoreDomainSS);



        //CAMBIOS GENERALES -----------------------------------------------------------------
        //Se agregan Settings a todos los Scorecards

        List<ScoreCard> listScoreCards = delegate.findScoreCard();
        //Settings Scorecards

        for (ScoreCard scoreCard : listScoreCards) {
            scoreCard.setResponsible(person);


            if (!(scoreCard instanceof Pointer)) {
                Settings setting = (Settings) Factory.getBean("Settings");
                setting.setType(Settings.Type.SCORECARD);
                setting.setScoreCard(scoreCard);
                delegate.createSettings(setting);

                scoreCard.setUnit("(%)");
                scoreCard.setSettings(setting);


            }
             delegate.updateScoreCard(scoreCard);

        }

        //SE AGREGAN RANGOS A TODOS LOS SCORECARDS
        //Rangos
        List<ScoreCard> listScoreCardsRangos = delegate.findScoreCard();
        Double arrayd[] = {100000D, .9D, .8D, -100000D};

        for (ScoreCard scoreCard : listScoreCardsRangos) {
            Settings settingSC = scoreCard.getSettings();

            int cont = 1;
            for (long j = 1; j <= 3; j++) {
                Colorimetry colorimetry = delegate.findColorimetry(j);
                ColorimetrySettings cs = (ColorimetrySettings) Factory.getBean("ColorimetrySettings");
                cs.setSettings(settingSC);
                cs.setColorimetry(colorimetry);
                cs.setMaxScore(arrayd[cont - 1]);
                cs.setMinScore(arrayd[cont]);
                delegate.createColorimetrySettings(cs);
                cont++;
            }
        }

        //Se crea el MAPA!!!!!
        //MapDrawing
        MapDrawing mapDrawing = (MapDrawing) delegate.findMapDrawing(1L);

        //Map
        Map map = (Map) delegate.findScoreCard(20L);

        map.setMapDrawing(mapDrawing);
        delegate.updateMap(map);

        mapDrawing.setMap(map);
        delegate.updateMapDrawing(mapDrawing);


        //Se agregan scorecards al MAPA

        //MapDrawingSettings
        ScoreCard objectiveScorecardO1 = delegate.findScoreCard(17L);

        MapDrawingSettings mpsPEDO1 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsPEDO1.setSettings(objectiveScorecardO1.getSettings());
        mpsPEDO1.setMapDrawing(mapDrawing);
        mpsPEDO1.setX(213D);
        mpsPEDO1.setY(207D);
        delegate.createMapDrawingSettings(mpsPEDO1);

        //MapDrawingSettings
        ScoreCard objectiveScorecardO2 = delegate.findScoreCard(18L);

        MapDrawingSettings mpsPEDO2 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsPEDO2.setSettings(objectiveScorecardO2.getSettings());
        mpsPEDO2.setMapDrawing(mapDrawing);
        mpsPEDO2.setX(213D);
        mpsPEDO2.setY(283D);
        delegate.createMapDrawingSettings(mpsPEDO2);

        //MapDrawingSettings
        ScoreCard objectiveScorecardPED = delegate.findScoreCard(20L);

        MapDrawingSettings mpsPED = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsPED.setSettings(objectiveScorecardPED.getSettings());
        mpsPED.setMapDrawing(mapDrawing);
        mpsPED.setX(660D);
        mpsPED.setY(60D);
        delegate.createMapDrawingSettings(mpsPED);


        //Se crea el MAPA SS!!!!
        //MapDrawing
        MapDrawing mapDrawingSS = (MapDrawing) delegate.findMapDrawing(2L);

        //Map
        Map mapSS = (Map) delegate.findScoreCard(21L);

        mapSS.setMapDrawing(mapDrawingSS);
        delegate.updateMap(mapSS);

        mapDrawingSS.setMap(mapSS);
        delegate.updateMapDrawing(mapDrawingSS);


        //Se agregan scorecards al MAPA

        //MapDrawingSettings
        ScoreCard objectiveScorecardO1SS = delegate.findScoreCard(12L);

        MapDrawingSettings mpsSSO1 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsSSO1.setSettings(objectiveScorecardO1SS.getSettings());
        mpsSSO1.setMapDrawing(mapDrawingSS);
        mpsSSO1.setX(55D);
        mpsSSO1.setY(250D);
        delegate.createMapDrawingSettings(mpsSSO1);

        //MapDrawingSettings
        ScoreCard objectiveScorecardO2SS = delegate.findScoreCard(13L);

        MapDrawingSettings mpsSSO2 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsSSO2.setSettings(objectiveScorecardO2SS.getSettings());
        mpsSSO2.setMapDrawing(mapDrawingSS);
        mpsSSO2.setX(245D);
        mpsSSO2.setY(220D);
        delegate.createMapDrawingSettings(mpsSSO2);


        


        //SE CREA SRM

        SRM srm1 = (SRM) Factory.getBean("SRM");
        srm1.setName("Junta de Gabinete");
        srm1.setPlace("Sala de Juntas 1");
        srm1.setResponsable("Gobierno de Oaxaca");
        srm1.setExpectedTime(2D);
        srm1.setSRMDate(ScoreCardUtils.generateDate(15, 8, 2011));
        srm1.setScoreView(scoreView);
        delegate.createSRM(srm1);

        /*Topic topic1 = (Topic) Factory.getBean("Topic");
        topic1.setDescription("Tema de Prueba");
        delegate.createTopic(topic1);

        TopicScoreView topicScoreView1 = (TopicScoreView) Factory.getBean("TopicScoreView");
        topicScoreView1.setScoreView(scoreView);
        topicScoreView1.setTopic(topic1);
        topicScoreView1.setType(TopicScoreView.Type.DELAYED);
        delegate.createTopicScoreView(topicScoreView1);*/


        return new ModelAndView("advancePopulate");
    }
}
